/**
 * 
 */
package com.suyx.job.service.area.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.suyx.job.entity.RoleInfo;
import com.suyx.job.entity.TuInfo;
import com.suyx.job.enums.SectEnums;
import com.suyx.job.utils.ImagePHash;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.ScreenCapture;
import com.suyx.job.utils.SysStaticData;

/**
 * @author syx
 *
 */
@Service
public class GoToDtgjAreaServiceImpl extends AbstractAreaService{

	public final static Map<String,String> options_x = new HashMap<>();  
	public final static Map<String,String> options_y = new HashMap<>();
	static {  
		options_x.put("0", "717 265");  
		options_x.put("1", "417 176");  
		options_x.put("2", "522 176");
		options_x.put("3", "622 176"); 
		options_x.put("4", "420 277");
		options_x.put("5", "518 277");
		options_x.put("6", "622 277");
		options_x.put("7", "417 366");
		options_x.put("8", "520 366");
		options_x.put("9", "620 366");
		
		options_y.put("0", "851 270");  
		options_y.put("1", "555 180");  
		options_y.put("2", "652 180");
		options_y.put("3", "750 180"); 
		options_y.put("4", "550 274");
		options_y.put("5", "656 274");
		options_y.put("6", "757 274");
		options_y.put("7", "550 373");
		options_y.put("8", "650 373");
		options_y.put("9", "752 373");
	}
	
	@Override
	public boolean execute(String device, String path, String area) {
		try {
			String[] infos = SysStaticData.CA1.get("大唐国境").split("&");
			userCA1(device,infos);
			// 到达国境
			ImagePHash p = new ImagePHash();
			ScreenCapture.snapshot(device,path, "screen.png");
	        ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
	                new FileOutputStream(path + device +".png"), 15,409,49,37);
	        String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
	        String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH +"chuangkou.png")));
	        int score9 =  p.distance(image1, image2);
	        if(score9 == 0){
	        	ScreenCapture.mouseClick(device, "392 492");
	        }
	        
			ScreenCapture.mouseClick(device, "70 667");
			Thread.sleep(2 * 1000L);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean support(String area) {
		return SectEnums.DTGJ.getCode().equals(area);
	}

	@Override
	public boolean goToPoint(RoleInfo roleInfo, String path, TuInfo tuInfo) {
		String device = roleInfo.getDevice();
		String area = tuInfo.getZuobiaoX()+" "+tuInfo.getZuobiaoY();
		try {
			Thread.sleep(2 * 1000L);
		
			ScreenCapture.mouseClick(device, "117 68");//打开地图
	        ScreenCapture.mouseClick(device, "420 60");// 选x坐标
	        String x = area.split(" ")[0];
	        String y = area.split(" ")[1];
	        input_x(device,x);
	        ScreenCapture.mouseClick(device, "720 366");
	        ScreenCapture.mouseClick(device, "570 72"); // 选y坐标
	        input_y(device, y);
	        ScreenCapture.mouseClick(device, "850 373");
	        ScreenCapture.mouseClick(device, "696 60");  // 前往
	        ScreenCapture.mouseClick(device, "1117 48"); //关闭地图
	        startGoto(roleInfo,path,tuInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	private void input_x(String device,String x){
		for(int i =0;i<x.length();i++){
			char c = x.charAt(i);
			String x_zb = options_x.get(String.valueOf(c));
			ScreenCapture.mouseClick(device, x_zb);
		}
	}
	
	private void input_y(String device,String y){
		for(int i =0;i<y.length();i++){
			char c = y.charAt(i);
			String y_zb = options_y.get(String.valueOf(c));
			ScreenCapture.mouseClick(device, y_zb);
		}
	}

}
